//ui.cpp
#include <math.h>
#include <sstream>
#include <vector>
#include "ui.h"
#include "gamedrawer.h"
#include <iostream>
#include <ctime>
#include <sys/timeb.h>

UI::UI(int w, int h){
	mouseX = 0;
	mouseY = 0;
	sbox = SelectionBox();
}

void UI::update(){
}

void UI::draw(){
	sbox.draw();
	string s = "Controls:";
	Coord pos = Coord(12, get_window_height()-24);
	stroke(Color(255,255,255));
	renderBitmapString( pos, &s);
	pos.y -= 14;
	s = "   Designate for gathering: Z";
	renderBitmapString( pos, &s);
	pos.y -= 14;
	s = "   Toggle Reactor on/off: X";
	renderBitmapString( pos, &s);
	pos.y -= 14;
	s = "   Convert Blueprint/Reactor: C";
	renderBitmapString( pos, &s);
	pos.y -= 14;
	s = "   Move camera: WASD";
	renderBitmapString( pos, &s);
}

void UI::handleMouseInput(){
	if(mouse_left()){
		sbox.tempStart = mouse_get_position();
	}
	else{
		sbox.endSelection();
	}

}
void UI::mouseDragged(){
	if(!sbox.active && mouse_left()){
		sbox.newSelection(sbox.tempStart);
		sbox.updateSelection(mouse_get_position());
	}
	else if (mouse_left()){
		sbox.updateSelection(mouse_get_position());
	}
}

void SelectionBox::newSelection(Coord in){
	active = true;
	start = in;
	end = in;
}
void SelectionBox::updateSelection(Coord in){
	end = in;
}
void SelectionBox::endSelection(){
	active = false;
}

void SelectionBox::draw(){
	if (active) {
		stroke(Color(0,255,0));
		fill(Color(100,255,100,20));
		drawRect(start,end);
		noFill();
		noStroke();
	}
}